<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/2/3
 * Time: 16:06
 */

namespace Api\Controller;
use Think\Controller;

class SmsController extends Controller
{
    //创蓝发送短信接口URL, 如无必要，该参数可不用修改
    const API_SEND_URL='http://sms.253.com/msg/HttpBatchSendSM';

    //创蓝短信余额查询接口URL, 如无必要，该参数可不用修改
    const API_BALANCE_QUERY_URL='http://sms.253.com/msg/QueryBalance';

    const API_ACCOUNT='N4900526';//创蓝账号 替换成你自己的账号

    const API_PASSWORD='Uhj5K87ku';//创蓝密码 替换成你自己的密码

    /**
     * 发送短信
     *
     * @param string $mobile 		手机号码
     * @param string $msg 			短信内容
     * @param string $needstatus 	是否需要状态报告
     */
    public function sendSMS( $mobile, $msg, $needstatus = 'false', $key) {
        if(md5('jinrongkuanggong'.$key) == $_SESSION['CheckKey']){
            $smsRecord = M('Sms_record');
            //查询此电话号码和ip是否满足条件
            $day = date('Y-m-d',time());
            $mobileCount = $smsRecord->where(array('mobile'=>$mobile, 'day' => $day))->count();
            $ipCount = $smsRecord->where(array('ip'=>get_client_ip(), 'day' => $day))->count();
            if($mobileCount >= 5 || $ipCount >= 10){//此电话号码已访问接口5次，或者此ip一访问接口10次，请明日再来
                return false;
            }
            $data = array(
                'ip' => get_client_ip(),
                'mobile' => $mobile,
                'msg' => $msg,
                'day' => date('Y-m-d',time()),
                'sendtime' => date('Y-m-d H:i:s',time())
            );
            //创蓝接口参数
            $postArr = array (
                'account' => self::API_ACCOUNT,
                'pswd' => self::API_PASSWORD,
                'msg' => $msg,
                'mobile' => $mobile,
                'needstatus' => $needstatus
            );
            $result = $this->curlPost( self::API_SEND_URL , $postArr);
            $result = $this->execResult($result);
            if(isset($result[1]) && $result[1]==0){//发送成功
                $smsRecord->add($data);
            }
            return $result;
        }
        else{
            return false;
        }
    }

    /**
     * 查询额度
     *
     *  查询地址
     */
    public function queryBalance() {

        //查询参数
        $postArr = array (
            'account' => self::API_ACCOUNT,
            'pswd' => self::API_PASSWORD,
        );
        $result = $this->curlPost(self::API_BALANCE_QUERY_URL, $postArr);
        return $result;
    }

    /**
     * 处理返回值
     *
     */
    public function execResult($result){
        $result=preg_split("/[,\r\n]/",$result);
        return $result;
    }

    /**
     * 通过CURL发送HTTP请求
     * @param string $url  //请求URL
     * @param array $postFields //请求参数
     * @return mixed
     */
    private function curlPost($url,$postFields){
        $postFields = http_build_query($postFields);
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
        $result = curl_exec ( $ch );
        curl_close ( $ch );
        return $result;
    }

    //魔术获取
    public function __get($name){
        return $this->$name;
    }

    //魔术设置
    public function __set($name,$value){
        $this->$name=$value;
    }

}



